package com.xuecheng.content.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.TeachplanDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanMediaVo;
import com.xuecheng.commons.model.vo.TeachplanVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-09-14
 */
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private TeachplanMapper teachplanMapper;
    @Override
    public ResponseResult treeNodes(Long courseId) {
        // 1.查询所有数据
        List<TeachplanVo> list = teachplanMapper.treeNodes(courseId);
        // 2.筛选一级
        TeachplanVo one = null;
        for (TeachplanVo vo : list) {
            if (vo.getGrade().equals(1)) {
                one = vo;
                break;
            }
        }

        // 3.筛选二级
        List<TeachplanVo> twoList = new ArrayList<>();
        for (TeachplanVo vo : list) {
            if (vo.getGrade().equals(2)) {
                twoList.add(vo);
            }
        }
        // 3-1 将二级关联到一级
        one.setTeachPlanTreeNodes(twoList);

        // 4.筛选三级
        for (TeachplanVo two : twoList) { // 遍历二级
            // 升级三级集合
            List<TeachplanVo> threeList = new ArrayList<>();
            for (TeachplanVo vo : list) {
                if (vo.getParentid().equals(two.getId())) { // 三级父id = 二级id
                    threeList.add(vo);
                }
            }
            // 将三级关联到二级
            two.setTeachPlanTreeNodes(threeList);
        }

        // 5.返回一级大纲
        return ResponseResult.okResult(one);
    }

    @Autowired
private CourseBaseService courseBaseService;


    // 新增或更新课程计划
    @Override
    public ResponseResult saveOrUpdateTeachPlan(TeachplanDto dto) {
        // 0. 校验参数合法性
        // 1.根据courseId查询课程数据
        CourseBase courseBase = courseBaseService.getById(dto.getCourseId());
        // 2.判断课程状态
        if (StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_COMMIT_STATUS)
                || StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PASTED_STATUS)
                || StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        // 3.dto->entity
        Teachplan teachplan = BeanHelper.copyProperties(dto, Teachplan.class);
        teachplan.setCreateDate(new Date()); // 创建时间
        // 4.判断课程是否为二级
        if (teachplan.getGrade().equals(2)) {
            // 4-1 查询一级大纲
            LambdaQueryWrapper<Teachplan> qw = new LambdaQueryWrapper<>();
            qw.eq(Teachplan::getCourseId,teachplan.getCourseId());
            qw.eq(Teachplan::getGrade,1);
            Teachplan one = this.getOne(qw);
            // 4-2 设置二级parentId
            teachplan.setParentid(one.getId());
        }
        // 5.判断课程是否为三级
        if (teachplan.getId() == null && teachplan.getGrade().equals(3)) {
            // 5-1 在原来任务数+1
            courseBase.setPlanNum(courseBase.getPlanNum()+1);
            // 5-2 更新数据库
            courseBaseService.updateById(courseBase);
        }

        // 6.保存或更新课程计划
        this.saveOrUpdate(teachplan);

        // 7.返回ok
        return ResponseResult.okResult();
    }

    // 删除课程计划
    @Override
    public ResponseResult deleteTeachplan(Long id) {
        // 1.查询课程计划
        Teachplan teachplan = this.getById(id);
        // 2.查询课程信息
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        // 3.判断课程状态
        if (StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_COMMIT_STATUS)
                || StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PASTED_STATUS)
                || StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        // 4.删除课程计划
        if (teachplan.getGrade().equals(2)) { // 二级
            // 判断二级下是否有三级小节
            LambdaQueryWrapper<Teachplan> qw = new LambdaQueryWrapper<>();
            qw.eq(Teachplan::getParentid, teachplan.getId());
            long count = this.count(qw);
            if (count > 0) {
                throw new BusinessException(ErrorCode.DELETEERROR);
            }
            this.removeById(id);

        } else { // 三级
            // 课程任务数量-1
            courseBase.setPlanNum(courseBase.getPlanNum() - 1);
            courseBaseService.updateById(courseBase);

            this.removeById(id);
        }
        // 5.返回ok
        return ResponseResult.okResult();
    }

}
